package com.kjlink.internal.workreport.project.model;


import cn.xuqiudong.common.base.model.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.kjlink.internal.workreport.base.annotation.NoneColumn;
import com.kjlink.internal.workreport.base.constant.CommonConstant;
import com.kjlink.internal.workreport.base.enums.ProjectType;
import com.kjlink.internal.workreport.helper.ReportCommonHelper;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.util.NumberUtils;

import java.math.BigDecimal;


/**
 * 项目 实体类
 *
 * @author Vic.xu
 */
public class Project extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 项目名称
     */
    private String name;

    /**
     * 预计工时
     */
    private Integer duration;


    /**
     * 备注
     */
    private String note;

    /**
     * 创建人：项目经理
     */
    @NoneColumn
    private String createName;

    /**
     * 状态
     */
    private String status;

    /**
     * jira前缀
     */
    private String jiraPrefix;

    /**
     * 当前项目已使用工时
     */
    private Integer usedTime;

    /**
     * 项目类型
     */
    private ProjectType type;

    /**
     * 预计工时 小时
     */
    @NoneColumn
    // 这里指定序列化和反序列化时的格式，比如保留两位小数
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal durationHour;

    /**
     * 已使用工时：小时
     */
    @NoneColumn
    // 这里指定序列化和反序列化时的格式，比如保留两位小数
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "0.00")
    private BigDecimal usedTimeHour;


    /***************** set|get  start **************************************/
    /**
     * set：项目名称
     */
    public Project setName(String name) {
        this.name = name;
        return this;
    }

    /**
     * get：项目名称
     */
    public String getName() {
        return name;
    }

    /**
     * set：所属公司
     */
    public Project setDuration(Integer duration) {
        this.duration = duration;
        return this;
    }

    /**
     * get：所属公司
     */
    public Integer getDuration() {
        return duration;
    }

    /**
     * set：备注
     */
    public Project setNote(String note) {
        this.note = note;
        return this;
    }

    /**
     * get：备注
     */
    public String getNote() {
        return note;
    }


    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getJiraPrefix() {
        return jiraPrefix;
    }

    public void setJiraPrefix(String jiraPrefix) {
        this.jiraPrefix = jiraPrefix;
    }

    public Integer getUsedTime() {
        return usedTime;
    }

    public void setUsedTime(Integer usedTime) {
        this.usedTime = usedTime;
    }

    public ProjectType getType() {
        return type;
    }

    public void setType(ProjectType type) {
        this.type = type;
    }

    public String getTypeText() {
        return type == null ? null : type.getText();
    }

    /***************** set|get  end **************************************/

    public BigDecimal getDurationHour() {
        if (duration != null && durationHour == null) {
            durationHour = ReportCommonHelper.toHoursNumber(duration);
        }
        return durationHour;
    }

    public void setDurationHour(BigDecimal durationHour) {
        if (durationHour != null) {
            duration = ReportCommonHelper.toMinutesNumber(durationHour).intValue();
        }
        this.durationHour = durationHour;
    }

    public BigDecimal getUsedTimeHour() {
        if (usedTime != null && usedTimeHour == null) {
            usedTimeHour = ReportCommonHelper.toHoursNumber(usedTime);
        }
        return usedTimeHour;
    }

    public void setUsedTimeHour(BigDecimal usedTimeHour) {
        if (usedTimeHour != null) {
            usedTime = ReportCommonHelper.toMinutesNumber(usedTimeHour).intValue();
        }
        this.usedTimeHour = usedTimeHour;
    }

    /**
     * 是否可编辑用户
     *
     * @return boolean
     */
    public boolean getCouldEdit() {
        User user = SessionHelper.currentUser();
        if (user == null) {
            return false;
        }
        if (Boolean.TRUE.equals(user.getDevManager())) {
            return true;
        }
        if (NumberUtils.equals(this.id, user.getId())) {
            return true;
        }
        return CommonConstant.SPECIAL_USER_CODE.equals(user.getUserCode());
    }

    public boolean getCouldDelete() {
        return SessionHelper.couldEdit(this.createId);
    }
}
